DiscGolf.Views['login'] = Backbone.View.extend( {

    events: { },

    initialize: function() {

        this.render();

        $( document ).on( 'keyup', $.proxy( this.checkForEnterPress, this ) );
    },

    render: function() {

        this.$el.height( this.options.height ).css( { 'position': 'relative' } );

        this.emailInput = this.makeEl('div');
        this.passwordInput = this.makeEl('div');

        this.innerContainer =
            this.makeEl('div').css( { 'position': 'absolute' } ).append(
                this.emailInput, this.passwordInput
            );

        this.$el.append( this.innerContainer );

        this.emailView =
            new DiscGolf.Views['input']( { 'text': 'Email : ',
                                           'el': this.emailInput,
                                           'textColor': 'white',
                                           'tabIndex': '1',
                                           'focus': this.options.focus,
                                           'preventEnter': true,
                                           'type': 'text' } );
        
        this.passwordView =
            new DiscGolf.Views['input']( { 'text': 'Password : ',
                                           'hideText': true,
                                           'el': this.passwordInput,
                                           'textColor': 'white',
                                           'tabIndex': '2',
                                           'type': 'text' } );
        
        DiscGolf.CommonLib.centerEl( { el: this.innerContainer, parentEl: this.$el } );

        return this;
    },

    checkForEnterPress: function( e ) {

        if( e.keyCode == 13 ) {

            this.options.golferModel.set( { 'emailAddress': this.emailView.getFieldValue(),
                                            'password': this.passwordView.getFieldValue() } );

            if( this.options.golferModel.isValid() ) {

                $.ajax( { url: DiscGolf.CommonLib.makeUrl( { controller: 'golfer', argList: [ 'login' ] } ),
                          data: { password: this.passwordView.getFieldValue(),
                                  emailAddress: this.emailView.getFieldValue() },
                          type: 'GET',
                          success: $.proxy( this.handleLogin, this ) } );
            } else {

                new DiscGolf.Views['modalBox']( { content: this.makeEl( 'div' ).css( { 'color': 'red' } ).text( this.options.golferModel.validationError ) } );
            }
        }
    },

    handleLogin: function( response ) {

        var responseObj = DiscGolf.CommonLib.evaluateJson( response );

        if( responseObj.error ) {

            var errorDialogue = new DiscGolf.Views['modalBox']( { content: this.makeEl( 'div' ).text( responseObj.error ) } );

            this.listenTo( errorDialogue, 'closed', this.handleModalClose );

            this.$el.on( 'keydown', 'div, input', $.proxy( this.preventTyping, this ) );

        } else {

            this.options.golferModel.set( { 'id': responseObj.id } );

            this.trigger( 'loggedIn' );
            this.$el.hide( 'blind', 2000, function() { $( this ).remove(); } );
        }

        $( document ).off( 'keyup', $.proxy( this.checkForEnterPress, this ) );
    },

    handleModalClose: function() {

        $( document ).on( 'keyup', $.proxy( this.checkForEnterPress, this ) );
        this.$el.off( 'keydown', 'div, input', $.proxy( this.preventTyping, this ) );
    },

    preventTyping: function( e ) {

        e.preventDefault(); 
    }

} );
